package mt.interfaceGui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import mt.Order;
import mt.comm.ClientComm;

public class SentinelaComprar implements ActionListener {
	private JFrame frameComprar;
	private String nomeUtilizador;
	private ConfiguracaoTabela tableModel;
	private ClientComm clientComm;

	public SentinelaComprar(String nomeUtilizador,	ConfiguracaoTabela tableModel, ClientComm clientComm) {
		this.nomeUtilizador=nomeUtilizador;
		this.tableModel=tableModel;
		this.clientComm=clientComm;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		frameComprar= new JFrame("Comprar");
		frameComprar.setSize(300,300);
		frameComprar.setLocationRelativeTo(null);
		frameComprar.setResizable(false);
		JButton done = new JButton("Done");


		JLabel nome = new JLabel("User: "+nomeUtilizador);

		JLabel stock = new JLabel("Insert Stock");
		final JTextField stockField= new JTextField();

		JLabel unit = new JLabel("Insert Unit");
		final JTextField unitField= new JTextField();

		JLabel price = new JLabel("Insert Price");
		final JTextField priceField= new JTextField();


		frameComprar.setLayout(new BorderLayout());
		JPanel panel1= new JPanel();
		panel1.setLayout(new GridLayout(7,1));

		panel1.add(nome);
		panel1.add(stock);
		panel1.add(stockField);
		panel1.add(unit);
		panel1.add(unitField);
		panel1.add(price);
		panel1.add(priceField);
	
		//done.addActionListener(new SentinelaDone(nomeUtilizador,stockField,unitField,priceField,tableModel,clientComm,frameComprar));

		frameComprar.add(panel1);
		frameComprar.add(done, BorderLayout.SOUTH);
		
		done.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				int unit1= Integer.parseInt(unitField.getText());
				double price1= Double.parseDouble(priceField.getText());
				Order order = Order.createBuyOrder(nomeUtilizador, stockField.getText(), unit1, price1);
				//tableModel.add(order);
				clientComm.sendOrder(order);
				frameComprar.setVisible(false);
				
				
			}
		});
		frameComprar.setVisible(true);
	}


}
